/*
 * Copyright 2011 Philippe Blanc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package me.l1k3.fx.client;

import me.l1k3.fx.client.channel.OpacityChannel;
import me.l1k3.fx.client.impl.FXShowAndHideAbstract;
import me.l1k3.fx.client.inter.FX;

import com.google.gwt.dom.client.Element;

public class FXFade extends FXShowAndHideAbstract {
	private OpacityChannel opacityChannel;
	
	public FXFade() {
		this(1, 0);
	}
	
	public FXFade(double start, double end) {
        opacityChannel = OpacityChannel.range(getType(), start, end);
        
    }
	
	protected FXFade(Element element, double start, double end) {
        opacityChannel = OpacityChannel.smartRange(getType(), element, start, end);
        
    }
	
	//- - - factory
	
	public final static FXFade range(Element element) {
        return FXFade.smartRange(element, 1, 0);
    }
	
	public final static FXFade range(Element element, double start, double end) {
	    FXFade fade = new FXFade(start, end);
	    fade.init(element);
	    return fade;
    }
	
	public final static FXFade smartRange(Element element, double start, double end) {
	    FXFade fade = new FXFade(element, start, end);
	    fade.init(element);
	    return fade;
	}
	
	//- - -
	
	@Deprecated
	public static void setOpacity(Element element, double opacity) {
	    OpacityChannel.setOpacity(element, opacity);
	}
	
	//- - -
	
	public void range(double start, double end) {
	    opacityChannel.setRange(start, end);
    }
	
	
    //- - -	
	
	@Override
	public boolean isVisible() {    
	    return opacityChannel.isForward();
	}

    //- - -

	/**
     * @exclude
     */
    @Override
    public FX init(Element element) {
        super.init(element);
        opacityChannel.init(context);
        validateChannel(opacityChannel);
        
        return this;
    }
	
    /**
     * @exclude
     */
	@Override
	public FX toggle() {
		opacityChannel.toggle(context);
		
		return this;
	}

	/**
     * @exclude
     */
	@Override
	public void onStart() {
		super.onStart();
		opacityChannel.start(context);
	}
	
	/**
     * @exclude
     */
    @Override
    public void onUpdate(double progress) {
        context.setProgress(progress);
        opacityChannel.update(context);
    }	
	
    /**
     * @exclude
     */
	@Override
	public void onComplete() {
		opacityChannel.complete(context);
		super.onComplete();
	}
}